using System;
using System.Collections;
using Scenario.WorkflowEngine.DataModel;

namespace Scenario.WorkflowEngine.DataModel.WorkflowDataTypes
{
	//*********************************************************************
	//
	// XmlDataTypes Class
	//
	// The XmlDataTypes class represents a collection base of XmlDataType
	// Its imports methods from WAPI.XmlObjectSet like get set and add
	// XmlDataTypes Has no fields
	//
	//*********************************************************************

	public class XMLDataTypes : bOS.XML.Data.XMLObjectSet 
	{
		public const string MAIN_NODE	= "DataTypes";

		/** FIELDS **/

		/** no fields **/

		/** CONSTRUCTORS **/

		/** CONSTRUCTOR Empty **/
		public XMLDataTypes()
		{
			this.Init();
		}

		/** CONSTRUCTOR Width xml parsering**/
		public XMLDataTypes(System.Xml.XmlNode nodeList)
		{
			this.Parse (nodeList);
		}

		/** INSTANCE METHODS **/

		/** Inizialize Method **/
		override public void Init()
		{
			this.RemoveAll();
		}

		override public void Parse(System.Xml.XmlNode nodeList)
		{
			this.Init();

			System.Xml.XmlNode node;

			for ( int i=0; i<nodeList.ChildNodes.Count;i++)
			{
				node = nodeList.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLDataType.MAIN_NODE:
						this.Add ( new XMLDataType(node));
						break;
				}
			}
		}

		/**Method to retive a nodelist from collection **/
		override public bOS.XML.XMLNode GetXMLNode()
		{
			bOS.XML.XMLNode xmlNode = new bOS.XML.XMLNode(XMLDataTypes.MAIN_NODE);

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}

			return xmlNode;
		}

		/** SORTING  FIELDS **/

		public enum SortFields 
		{
			Key,
			Type,
			Description
		}

		/** SORTING METHODS **/


		public void Sort(SortFields sortField, bool isAscending) 
		{
			switch (sortField) 
			{
				case SortFields.Key:
					InnerList.Sort(new KeyComparer());
					break;
				case SortFields.Type:
					InnerList.Sort(new TypeComparer());
					break;
				case SortFields.Description:
					InnerList.Sort(new DescriptionComparer());
					break;
				
			}
			if (!isAscending) InnerList.Reverse();
		}


		private sealed class KeyComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLDataType first = (XMLDataType) x;
				XMLDataType second = (XMLDataType) y;
				return first.Key.CompareTo(second.Key);
			}
		}

		private sealed class TypeComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLDataType first = (XMLDataType) x;
				XMLDataType second = (XMLDataType) y;
				return first.Type.CompareTo(second.Type);
			}
		}

		private sealed class DescriptionComparer : IComparer 
		{
			public int Compare(object x, object y) 
			{
				XMLDataType first = (XMLDataType) x;
				XMLDataType second = (XMLDataType) y;
				return first.Description.CompareTo(second.Description);
			}
		}

	}
}